package org.aiforum.backend.controller;

import java.util.List;

import org.aiforum.backend.common.QueryPreperty;
import org.aiforum.backend.common.Response;
import org.aiforum.backend.service.ContactService;
import org.aiforum.domain.Contact;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 联系我们控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/contact")
public class ContactController {
      
	@Autowired
	private ContactService contactService;
	
	@RequestMapping(value={"/find/{contactId}"},method={RequestMethod.GET})
	public ModelAndView find(@PathVariable Integer contactId){
		Contact contact=contactService.find(contactId);
		ModelAndView modelAndView=new ModelAndView();
	    modelAndView.setViewName("contact/details");
	    modelAndView.addObject("contact", contact);
		return modelAndView;
	}
	
	@RequestMapping(value={"/delete/{contactId}"},method={RequestMethod.GET})
	public ModelAndView delete(@PathVariable Integer contactId){
		Response response=contactService.delete(contactId);
		ModelAndView modelAndView=new ModelAndView();
	    modelAndView.setViewName("redirect:../../message");
	    modelAndView.addObject("message", response.getMessage());
		return modelAndView;
	}
	
	
	@RequestMapping(value={"/querys"})
	public ModelAndView  querys(QueryPreperty queryPreperty,PageInfo page){
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), true);
		
		List<Contact> contacts=contactService.querys(queryPreperty);
		page=new PageInfo<Contact>(contacts);
		
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.setViewName("contact/list");
		modelAndView.addObject("contacts", contacts);
		modelAndView.addObject("queryPreperty", queryPreperty);
		modelAndView.addObject("page", page);
		return modelAndView;
	}
}
